<?php

namespace app\admin\controller\polite;

use app\admin\model\PoliteImpress;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use think\App;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\annotation\ControllerAnnotation;
use think\facade\Db;
use function GuzzleHttp\default_user_agent;

/**
 * Class Goods
 * @package app\admin\controller\polite
 * @ControllerAnnotation(title="乡镇印象管理")
 */
class Impress extends AdminController
{

    use Curd;

    protected $relationSearch = false;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new PoliteImpress();
    }

    /**
     * @NodeAnotation(title="乡镇印象列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $admin=session('admin');
            if($admin['type']==3){
                //乡镇管理员，显示自己乡镇的数据
                $where[]=['township','=',$admin['area_id']];
            }
            $count = $this->model->where($where)->count();
            $list = $this->model->where($where)->page($page, $limit)->order($this->sort)->select();
            foreach ($list as $k=>$v){
                $list[$k]=$this->resetInfo($v);
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        $row=$this->resetInfo($row);
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'area|面积'   => 'require',
                'under_region_num|下辖地区/行政村数'  => 'require',
                'person_num|人口数'  => 'require',
                'contact_phone|联系电话'  => 'require',
                'files|轮播视频/图片'  => 'require',
                'introduce_brief|乡镇简介'  => 'require',
                'location_brief|地理位置简介'  => 'require',
                'person_brief|人口简介'  => 'require',
            ];
            $this->validate($post, $rule);

            $files = explode('|',$post['files']);
            if (count($files) > 10){
                $this->error('轮播视频/图片最多不超过10张');
            }

            $post['township']=$this->admin['area_id'];
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $this->assign([
            'id'          => $id,
            'row'         => $row,
        ]);
        return $this->fetch();
    }

    private function resetInfo($data){
        if($data){
            if(!empty($data['township']))$data['township_text']=$this->getArea($data['township'])['name'];
            //$data['type_text']=!empty($data['type'])? (new \app\admin\service\PoliteConfigService)->getConfigById($data['type'])['config_value']:'';
        }
        return $data;
    }
}